package org.jfinancegraph.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import org.jfinanceobserver.persistence.ConnFactory;

/**
 * This JDialog is to Open WHDB
 *
 * $Author: r78v10a07@gmail.com $ $LastChangedDate: 2012-08-31 15:46:36 +0200
 * (Fri, 31 Aug 2012) $ $LastChangedRevision: 34 $
 *
 * @since Feb 14, 2013
 */
public class OpenDB extends javax.swing.JDialog {

    /**
     * Creates new form OpenWHDB
     *
     * @param parent the parent frame
     * @param modal true to be active
     */
    public OpenDB(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
        jBOpen.requestFocusInWindow();
    }

    /**
     * Returns if the opened DB is selected as MainDB
     *
     * @return true if the opened DB is selected as MainDB
     */
    public boolean isMain() {
        return main;
    }

    /**
     * Return the URL opened
     *
     * @return the URL opened
     */
    public String getUrl() {
        return url;
    }

    /**
     * Return true if the operation was canceled by the user
     *
     * @return true if the operation was canceled by the user
     */
    public boolean isCanceled() {
        return canceled;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jCDBMS = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLDriver = new javax.swing.JLabel();
        jTDriver = new javax.swing.JTextField();
        jTDriver.addActionListener(
            new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    jBOpenGlobalActionPerformed(e);
                }
            });
            jLServer = new javax.swing.JLabel();
            jTServer = new javax.swing.JTextField();
            jLPort = new javax.swing.JLabel();
            jTPort = new javax.swing.JTextField();
            jLDB = new javax.swing.JLabel();
            jTDB = new javax.swing.JTextField();
            jLUser = new javax.swing.JLabel();
            jTUser = new javax.swing.JTextField();
            jLPasswd = new javax.swing.JLabel();
            jPassword = new javax.swing.JPasswordField();
            jBCancel = new javax.swing.JButton();
            jBOpen = new javax.swing.JButton();

            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Sign In");

            jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
            jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel1.setText("Open Schema");

            jCDBMS.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MySQL" }));

            jLabel2.setText("DBMS:");

            jLDriver.setText("Driver:");

            jTDriver.setText("com.mysql.jdbc.Driver");

            jLServer.setText("Server:");

            jTServer.setText("localhost");
            jTServer.addActionListener(
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        jBOpenGlobalActionPerformed(e);
                    }
                }
            );

            jLPort.setText("Port:");

            jTPort.setText("3306");
            jTPort.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    jBOpenGlobalActionPerformed(e);
                }
            });

            jLDB.setText("Database:");

            jTDB.setText("finance");
            jTDB.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    jBOpenGlobalActionPerformed(e);
                }
            });

            jLUser.setText("User:");

            jTUser.setText("finance");
            jTUser.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    jBOpenGlobalActionPerformed(e);
                }
            });

            jLPasswd.setText("Password:");

            jPassword.setText("mypass");
            jPassword.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    jBOpenGlobalActionPerformed(e);
                }
            });

            jBCancel.setText("Cancel");
            jBCancel.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jBCancelActionPerformed(evt);
                }
            });

            jBOpen.setText("Open");
            jBOpen.requestFocusInWindow();
            jBOpen.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jBOpenActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator1)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(0, 0, Short.MAX_VALUE)
                                    .addComponent(jBOpen)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jBCancel))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLDB)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTDB))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLUser)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTUser, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLPasswd)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
                                    .addGap(42, 42, 42))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLDriver)
                                        .addComponent(jLServer))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jTServer)
                                            .addGap(6, 6, 6)
                                            .addComponent(jLPort)
                                            .addGap(4, 4, 4)
                                            .addComponent(jTPort, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jTDriver))))
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(0, 98, Short.MAX_VALUE)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jCDBMS, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(69, 69, 69))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jCDBMS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLDriver)
                        .addComponent(jTDriver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLServer)
                        .addComponent(jTServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLPort)
                        .addComponent(jTPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLDB)
                        .addComponent(jTDB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLUser)
                        .addComponent(jTUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLPasswd)
                        .addComponent(jPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jBOpen)
                        .addComponent(jBCancel))
                    .addContainerGap())
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

    private void jBOpenGlobalActionPerformed(java.awt.event.ActionEvent evt) {
        if (!jTDriver.getText().isEmpty()
                && !jTServer.getText().isEmpty()
                && !jTPort.getText().isEmpty()
                && !jTDB.getText().isEmpty()
                && !jTUser.getText().isEmpty()
                && jPassword.getPassword().length != 0) {
            jBOpenActionPerformed(evt);
        } else if (jTDriver.getText().isEmpty()) {
            jTDriver.requestFocusInWindow();
        } else if (jTServer.getText().isEmpty()) {
            jTServer.requestFocusInWindow();
        } else if (jTPort.getText().isEmpty()) {
            jTPort.requestFocusInWindow();
        } else if (jTDB.getText().isEmpty()) {
            jTDB.requestFocusInWindow();
        } else if (jTUser.getText().isEmpty()) {
            jTUser.requestFocusInWindow();
        } else if (jPassword.getPassword().length == 0) {
            jPassword.requestFocusInWindow();
        }
    }

    private void jBCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelActionPerformed
        canceled = true;
        dispose();
    }//GEN-LAST:event_jBCancelActionPerformed

    private void jBOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBOpenActionPerformed
        if (jCDBMS.getSelectedItem().equals("MySQL")) {
            url = "jdbc:mysql://" + jTServer.getText() + ":" + jTPort.getText() + "/" + jTDB.getText();
            ConnFactory.getInstance().setDriver(jTDriver.getText());
            ConnFactory.getInstance().setUrl(url);
            ConnFactory.getInstance().setPasswd(new String(jPassword.getPassword()));
            ConnFactory.getInstance().setUser(jTUser.getText());
            ConnFactory.getInstance().connect();
            
            if (!ConnFactory.getInstance().getFactory().isOpen()) {
                int type = JOptionPane.ERROR_MESSAGE;
                JOptionPane.showMessageDialog(this, "The URL: " + url + " is open", "Error", type);
            } else {
                dispose();
            }
        }
    }//GEN-LAST:event_jBOpenActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCancel;
    private javax.swing.JButton jBOpen;
    private javax.swing.JComboBox jCDBMS;
    private javax.swing.JLabel jLDB;
    private javax.swing.JLabel jLDriver;
    private javax.swing.JLabel jLPasswd;
    private javax.swing.JLabel jLPort;
    private javax.swing.JLabel jLServer;
    private javax.swing.JLabel jLUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPassword;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTDB;
    private javax.swing.JTextField jTDriver;
    private javax.swing.JTextField jTPort;
    private javax.swing.JTextField jTServer;
    private javax.swing.JTextField jTUser;
    // End of variables declaration//GEN-END:variables
    private boolean main = false;
    private String url;
    private boolean canceled = false;
    private java.awt.Frame parent;
}
